package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.DeptDao;
import com.itheima.domain.Dept;
import com.itheima.service.DeptService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class DeptServiceImpl implements DeptService {
    @Override
    public PageInfo<Dept> findByPage(int currentPage, int pageSize) {
        PageInfo<Dept> page = null;
        SqlSession sqlSession = null;
        try {
            //1 通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 通过MybatisUtil工厂类获取dao接口的代理对象
            DeptDao mapper = MybatisUtil.getMapper(sqlSession, DeptDao.class);
            //3 执行操作释放资源
            //3.1 在查询所有之前设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            List<Dept> list = mapper.findAll();
             page = new PageInfo<>(list);
            //MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return page;
    }

    @Override
    public void save(Dept cept) {
        SqlSession sqlSession=null;
        try {
             sqlSession = MybatisUtil.getSqlSession();
            DeptDao mapper = MybatisUtil.getMapper(sqlSession, DeptDao.class);
            //生成一个唯一的id，保存到cept
            String id = UUID.randomUUID().toString().replace("-", "");
            cept.setId(id);
            mapper.save(cept);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public Dept findById(String id) {
        Dept cept = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            DeptDao mapper = MybatisUtil.getMapper(sqlSession, DeptDao.class);

            cept =  mapper.findById(id);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return cept;
    }

    @Override
    public void update(Dept cept) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            DeptDao mapper = MybatisUtil.getMapper(sqlSession, DeptDao.class);

            mapper.update(cept);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            DeptDao mapper = MybatisUtil.getMapper(sqlSession, DeptDao.class);

            Stream.of(ids).forEach(mapper::delete);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Dept> findAll() {
       List<Dept>  list = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            DeptDao mapper = MybatisUtil.getMapper(sqlSession, DeptDao.class);

            list =  mapper.findAll();
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }
}
